#include "fd_repair.h"

uchar pong_expected         [] = { 0x7, 0x0, 0x0, 0x0, 0x69, 0x7b, 0x42, 0x55, 0xa4, 0x4, 0x1f, 0xe9, 0x9d, 0xc, 0x37, 0x9f, 0x76, 0xf, 0x69, 0x1a, 0xbe, 0x2f, 0xe2, 0x37, 0xad, 0xc3, 0x6c, 0xf8, 0x92, 0xca, 0xde, 0xdb, 0x15, 0xdb, 0xfe, 0x41, 0x99, 0x7e, 0x10, 0xaf, 0x65, 0x3b, 0x1e, 0x32, 0xaf, 0xf5, 0xa4, 0x3, 0x26, 0xa9, 0xd6, 0xdb, 0xfe, 0x80, 0x97, 0xa, 0x24, 0x92, 0x3f, 0xfb, 0x19, 0xf4, 0x44, 0x9, 0xe1, 0x5c, 0xd, 0xe1, 0xd6, 0xdf, 0x84, 0x4e, 0x1b, 0x18, 0xd5, 0x1d, 0xda, 0x9, 0x31, 0xde, 0x6b, 0xd6, 0x79, 0xc3, 0x4, 0x34, 0x7e, 0x52, 0x2a, 0x1, 0xd0, 0xad, 0x2d, 0xf8, 0xe9, 0xb1, 0xfb, 0x6a, 0x5d, 0x6d, 0xa, 0xa8, 0xc4, 0xb9, 0x60, 0xa6, 0xb3, 0x4f, 0xf8, 0xa6, 0x2c, 0xf4, 0xfc, 0xbe, 0x33, 0xbb, 0xca, 0x68, 0x5e, 0xa6, 0xe5, 0x78, 0x6f, 0xf3, 0xff, 0xf0, 0x74, 0x74, 0x94, 0x99, 0xa2, 0xe };
uchar shred_expected        [] = { 0x8, 0x0, 0x0, 0x0, 0xa1, 0xda, 0xa9, 0x32, 0x46, 0x77, 0xae, 0x9b, 0x54, 0x14, 0x33, 0xde, 0x9e, 0x10, 0x7c, 0x4, 0xe4, 0x4f, 0x34, 0x7c, 0x48, 0x7b, 0x68, 0xad, 0xb7, 0xdb, 0x40, 0x87, 0x2a, 0x30, 0x52, 0x4e, 0x26, 0x51, 0x6c, 0x27, 0xa2, 0x6d, 0x29, 0x5, 0xe9, 0xea, 0x4f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15, 0xdb, 0xfe, 0x41, 0xa2, 0xae, 0x51, 0xa2, 0xba, 0xa4, 0xbe, 0x20, 0x24, 0x41, 0xf, 0xb3, 0xdb, 0xde, 0xa, 0x9b, 0xee, 0x12, 0x40, 0x2f, 0x12, 0x46, 0x78, 0x2b, 0xac, 0x2e, 0x87, 0x35, 0x3e, 0x53, 0xea, 0x4a, 0x20, 0x87, 0xf7, 0x64, 0x97, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x9a, 0x96, 0xb, 0x0, 0x0, 0x0, 0x0, 0x0 };

/* Randomly generated by solana-keygen. */

uchar keys[ 64 ] = { 0x69, 0x7b, 0x42, 0x55, 0xa4, 0x4, 0x1f, 0xe9, 0x9d, 0xc, 0x37, 0x9f, 0x76, 0xf, 0x69, 0x1a, 0xbe, 0x2f, 0xe2, 0x37, 0xad, 0xc3, 0x6c, 0xf8, 0x92, 0xca, 0xde, 0xdb, 0x15, 0xdb, 0xfe, 0x41,  /* public key */
                     0x32, 0xa2, 0x33, 0x9a, 0x1d, 0xc1, 0x65, 0x6, 0x22, 0x77, 0x74, 0x5d, 0x9d, 0xdb, 0x42, 0xae, 0x44, 0xae, 0x5d, 0xec, 0x55, 0xd3, 0xb2, 0x21, 0xce, 0xee, 0x69, 0xac, 0x72, 0x66, 0xac, 0x56 /* private key */  };

fd_repair_t
setup( void ) {
  fd_repair_t repair;
  memcpy( repair.identity_key.uc, keys, sizeof(fd_pubkey_t) );
  repair.sign_fn  = fd_repair_sign_ed25519;
  repair.sign_ctx = keys;
  return repair;
}

void
test_pong( void ) {
  fd_repair_t       repair     = setup();
  fd_hash_t         ping_token = { .hash = { 0x86, 0xfa, 0xb4, 0xd0, 0x8, 0x1d, 0x51, 0x19, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 } };

  fd_repair_msg_t * pong       = fd_repair_pong( &repair, &ping_token );
  FD_TEST( 0==memcmp( pong, &repair.msg,   sizeof(fd_repair_msg_t) ) );
  FD_TEST( 0==memcmp( pong, pong_expected, sizeof(pong_expected)   ) );
  uchar buf[ sizeof(fd_repair_msg_t) ];
  FD_TEST( 0==memcmp( fd_repair_serialize( pong, buf ), pong_expected, sizeof(pong_expected) ) );
}

void
test_shred( void ) {
  fd_repair_t repair    = setup();
  fd_pubkey_t to        = { .uc = { 0xa2, 0xae, 0x51, 0xa2, 0xba, 0xa4, 0xbe, 0x20, 0x24, 0x41, 0xf, 0xb3, 0xdb, 0xde, 0xa, 0x9b, 0xee, 0x12, 0x40, 0x2f, 0x12, 0x46, 0x78, 0x2b, 0xac, 0x2e, 0x87, 0x35, 0x3e, 0x53, 0xea, 0x4a } };
  ulong       ts        = 0x1976558199b;
  uint        nonce     = 0x9c;
  ulong       slot      = 0xbd3b7;
  ulong       shred_idx = 0;

  fd_repair_msg_t * shred = fd_repair_shred( &repair, &to, ts, nonce, slot, shred_idx );
  FD_TEST( 0==memcmp( shred, &repair.msg,    sizeof(fd_repair_msg_t) ) );
  FD_TEST( 0==memcmp( shred, shred_expected, sizeof(fd_repair_msg_t) ) );
  uchar buf[ sizeof(fd_repair_msg_t) ];
  FD_TEST( 0==memcmp( fd_repair_serialize( shred, buf ), shred_expected, sizeof(shred_expected) ) );
}

void
test_highest_shred( void ) {
  fd_repair_t repair    = setup();
  fd_pubkey_t to        = { .uc = { 0xa, 0x27, 0xdb, 0x8a, 0xad, 0xa2, 0xc9, 0xfe, 0x47, 0x69, 0xe, 0xf9, 0x25, 0x4f, 0xf2, 0x71, 0x82, 0xa1, 0x4f, 0xec, 0xdd, 0x7c, 0xa2, 0x0, 0x82, 0xda, 0x9a, 0x6b, 0xdd, 0x41, 0x62, 0xb6 } };
  ulong       ts        = 0x19765532eee;
  uint        nonce     = 0xb3ec;
  ulong       slot      = 0xbd155;
  ulong       shred_idx = 0;

  fd_repair_msg_t * highest_shred = fd_repair_highest_shred( &repair, &to, ts, nonce, slot, shred_idx );
  FD_TEST( 0==memcmp( highest_shred, &repair.msg,            sizeof(fd_repair_msg_t)        ) );
  FD_TEST( 0==memcmp( highest_shred, highest_shred_expected, sizeof(highest_shred_expected) ) );
  uchar buf[ sizeof(fd_repair_msg_t) ];
  FD_TEST( 0==memcmp( fd_repair_serialize( highest_shred, buf ), highest_shred_expected, sizeof(highest_shred_expected) ) );
}

void
test_orphan( void ) {
  fd_repair_t repair = setup();
  fd_pubkey_t to     = { .uc = { 0xa2, 0xae, 0x51, 0xa2, 0xba, 0xa4, 0xbe, 0x20, 0x24, 0x41, 0xf, 0xb3, 0xdb, 0xde, 0xa, 0x9b, 0xee, 0x12, 0x40, 0x2f, 0x12, 0x46, 0x78, 0x2b, 0xac, 0x2e, 0x87, 0x35, 0x3e, 0x53, 0xea, 0x4a } };
  ulong ts           = 0x19764f78720;
  uint  nonce        = 0x0;
  ulong slot         = 0xb969a;

  fd_repair_msg_t * orphan = fd_repair_orphan( &repair, &to, ts, nonce, slot );
  FD_TEST( 0==memcmp( orphan, &repair.msg,     sizeof(fd_repair_msg_t) ) );
  FD_TEST( 0==memcmp( orphan, orphan_expected, sizeof(orphan_expected) ) );
  uchar buf[ sizeof(fd_repair_msg_t) ];
  FD_TEST( 0==memcmp( fd_repair_serialize( orphan, buf ), orphan_expected, sizeof(orphan_expected) ) );
}

int
main( int argc, char ** argv ) {
  fd_boot( &argc, &argv );

  test_pong();
  test_shred();
  test_highest_shred();
  test_orphan();

  fd_halt();
}
